package com.gavin.book.controller;

import com.gavin.book.constant.Constants;
import com.gavin.book.model.UserInfo;
import com.gavin.book.service.UserInfoservice;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserInfoservice userInfoservice;

    @RequestMapping("/login")
    public Boolean login(String name, String password, HttpSession session){
        //1.参数校验
        //2.校验账号和密码是否正确
        //3.设置session
        //4.返回结果
        if(!StringUtils.hasLength(name)||!StringUtils.hasLength(password)){
            return false;
        }
        //根据用户名,获取用户信息
        UserInfo userInfo=userInfoservice.queryUserInfoByName(name);
        if(userInfo==null){
            return false;
        }
        if(password.equals(userInfo.getPassword())){
            userInfo.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KEY,userInfo);
            return true;
        }

//        if("admin".equals(name)&&"123456".equals(password)){
//            session.setAttribute("userName",name);
//            return true;
//        }


        return false;
    }
}
